package cn.iocoder.yudao.module.brokerage.merchant.dao;

import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.module.brokerage.merchant.pojo.bo.BrokerageMerchantInviteSumBO;
import cn.iocoder.yudao.module.brokerage.merchant.pojo.po.AgencyBrokerageRecordDO;
import cn.iocoder.yudao.module.brokerage.merchant.pojo.vo.AppBrokerageMerchantRecordQueryDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 推广商家分佣记录 Mapper
 *
 * @author 山野羡民
 */
@Mapper
public interface BrokerageMerchantRecordMapper extends CoreMapper<AgencyBrokerageRecordDO> {

    default List<AgencyBrokerageRecordDO> selectListByStatusAndLessThanUnfreezeTime(Integer status, LocalDateTime unfreezeTime) {
        return selectList(new LambdaQueryWrapper<AgencyBrokerageRecordDO>()
                .eq(AgencyBrokerageRecordDO::getStatus, status)
                .lt(AgencyBrokerageRecordDO::getUnfreezeTime, unfreezeTime));
    }

    default int updateByIdAndStatus(Long id, Integer status, AgencyBrokerageRecordDO updateObj) {
        return update(updateObj, new LambdaQueryWrapper<AgencyBrokerageRecordDO>()
                .eq(AgencyBrokerageRecordDO::getId, id)
                .eq(AgencyBrokerageRecordDO::getStatus, status));
    }

    @Select("""
            SELECT source_shop_id, SUM(bonus_money) AS invite_bonus_money, SUM(base_price) AS invite_order_price
            FROM brokerage_merchant_record
            WHERE user_id = #{userId}
            GROUP BY source_shop_id
            """)
    List<BrokerageMerchantInviteSumBO> selectInviteSumByUserId(@Param("userId") Long userId);

    default AgencyBrokerageRecordDO selectByUserIdAndOrderItemId(Long userId, String tradeNo, Long orderItemId) {
        return selectOne(new CoreLambdaQueryWrapper<AgencyBrokerageRecordDO>()
                .eq(AgencyBrokerageRecordDO::getUserId, userId)
                .eq(AgencyBrokerageRecordDO::getTradeNo, tradeNo)
                .eq(AgencyBrokerageRecordDO::getOrderItemId, orderItemId)
                .limitN(1));
    }

    default List<AgencyBrokerageRecordDO> selectByTradeNo(String tradeNo) {
        return selectList(new LambdaQueryWrapper<AgencyBrokerageRecordDO>()
                .eq(AgencyBrokerageRecordDO::getTradeNo, tradeNo));
    }

    default Integer selectNumberSum(Long userId, AppBrokerageMerchantRecordQueryDTO param) {
        CoreLambdaQueryWrapper<AgencyBrokerageRecordDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.select(AgencyBrokerageRecordDO::getBonusMoney);
        wrapper.eqIfPresent(AgencyBrokerageRecordDO::getUserId, userId);
        wrapper.eqIfPresent(AgencyBrokerageRecordDO::getStatus, param.getStatus());
        wrapper.eqIfPresent(AgencyBrokerageRecordDO::getTradeNo, param.getTradeNo());
        wrapper.betweenIfPresent(AgencyBrokerageRecordDO::getUnfreezeTime, param.getUnfreezeTime());
        List<Integer> objs = selectObjs(wrapper, AgencyBrokerageRecordDO::getBonusMoney);
        return objs.stream().reduce(0, Integer::sum);
    }

}